package org.opens.javaskill.jackson.serialize;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.text.NumberFormat;

public class NumberSerializer extends JsonSerializer<Double> {

    @Override
    public void serialize(Double value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        NumberFormat instance = NumberFormat.getInstance();
        //控制最大小数位数为4位
        instance.setMaximumFractionDigits(4);
        String format = instance.format(value);
        jsonGenerator.writeString(format);
    }

}
